import UpLoadComponent from '../../components/iUpload/Index.vue'
import Vue from 'vue'

let $vm

function createVM (Vue) {
  const Upload = Vue.extend(UpLoadComponent)
  const upload = document.getElementById('upload-img-component')
  if (upload) {
    document.body.removeChild(upload)
  }
  $vm = new Upload({
    el: document.createElement('div')
  })
  document.body.appendChild($vm.$el)
  return $vm
}

function load ($vm, options) {
  for (let i in options) {
    $vm[i] = options[i]
  }
}

function addClass (el, className) {
  if (el.classList) {
    el.classList.add(className)
  } else {
    el.className += ' ' + className
  }
}

const manager = {}
export default {
  load (options, e) {
    $vm = createVM(Vue)
    // 去除fastclick
    e && addClass(e.target, 'needsclick')
    load.call(manager, $vm, options)
    setTimeout(() => {
      $vm.openUploadPop()
    }, 100)
  }
}
